const axios = require('axios');
const dayjs = require('dayjs');
const qs = require('querystringify');
let config = {}
process.nextTick(() => {
    config = {
        fujin: {
            userbid: process.env.FUJIN_USER_BID
        },
        yingmenkou: {
            userbid: process.env.YINGMENKOU_USER_BID
        },
        huarun: {
            userbid: process.env.HUARUN_USER_BID
        },
        hongguangli: {
            userbid: process.env.HONGGUANGLI_USER_BID
        }
    } 
})
const ins = axios.create({
    headers: {
        'x-request-platform': 'WEB',
        'x-request-product': 'X',
        'x-reuqest-appid': 'BOSMDUEOZ86S',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
        snbid: 'yunyoudianjing_%E5%B4%94%E6%80%BB'
    }
})

const runOTA = async (store, startTime, endTime) => {
    // 获取团购数据
    try {
        const otaData = await ins.post(process.env.OTA_URL, {
            startTime,
            otaType: null,
            pageNum: 1,
            pageSize: 100,
            snbid: 'yunyoudianjing_%E5%B4%94%E6%80%BB',
            endTime,
            type: 'count'
        }, {
            headers: {
                'x-request-userbid': config[store].userbid,
                Authorization: process.env.WX_USER_TOKEN,
            }
        })
        return otaData.data.result.list;
    } catch(e) {
        console.log('error', e)
    }
}

const runArea = async (store, startTime, endTime) => {
    // 获取区域数据
    const areaData = await ins.post(process.env.REGION_DATA_URL, {
        startTime,
        snbid: 'yunyoudianjing_%E5%B4%94%E6%80%BB',
        endTime,
    }, {
        headers: {
            'x-request-userbid': config[store].userbid,
            Authorization: process.env.WX_USER_TOKEN,
        }
    })
    return areaData.data.result;
}
const runCatering = async (store, startTime, endTime) => {
    // 获取餐饮数据
    const cateringData = await ins.get(process.env.CATERING_DATA_URL + qs.stringify({
        // snbid: 'yunyoudianjing_%E5%B4%94%E6%80%BB',
        page: 1,
        limit: 500,
        is_jb_time: 0,
        start_date: startTime,
        end_date: endTime,
        day_time: '',
        show_product_type: '',
        product_type: '',
        is_share: -1,
        pay_type: -1,
        order_type: -1,
        wf_status: -1,
        is_cash_prize_product: -1,
        syy_uid: '',
        is_cx: '',
        product_name: '',
        order_id: '',
        order_remark: '',
        is_wrhj_open: 0
    }, true), {
        headers: {
            'x-request-userbid': config[store].userbid,
            Authorization: process.env.WX_USER_TOKEN,
            referer: 'https://xyg.fs028.com/Wb/Data/saleList',
            accept: 'application/json, text/javascript, */*; q=0.01',
            cookie: `PHPSESSID=${process.env.FS_SESSION_ID}`,
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            "x-requested-with": "XMLHttpRequest"
        }
    })
    return cateringData.data.data;
}

const runRank = async (store, startTime, endTime) => {
    // 获取充送数据
    const rankData = await ins.post(process.env.RANK_DATA_RUL, {
        queryBeginTime: startTime,
        snbid: 'yunyoudianjing_%E5%B4%94%E6%80%BB',
        queryEndTime: endTime,
        pageNum: 1,
        pageSize: 100,
        queryParam: {
            ascOrDesc: 'desc',
            orderBy: 'totalNum'
        },  
        isSelectAll: false
    }, {
        headers: {
            'x-request-userbid': config[store].userbid,
            Authorization: process.env.WX_USER_TOKEN,
        }
    })
    return rankData.data.result.list;
}

const runBill = async (store, startTime, endTime) => {
    try {  
        const rankData = await ins.get(process.env.BILL_DATA_URL + qs.stringify({
            dtBeginTime: dayjs(startTime).format('YYYY-MM-DD'),
            dtEndTime: dayjs(endTime).format('YYYY-MM-DD'),
            dateOrShift: 'date',
            pattern: 1,
            all: false,
            pageNum: 1,
            pageSize: 30,
        }, true) + "&snbid=yunyoudianjing_%E5%B4%94%E6%80%BB", {
            headers: {
                'x-request-userbid': config[store].userbid,
                Authorization: process.env.WX_USER_TOKEN,
                host: 'qian.54laoban.com',
                referer: 'https://qian.54laoban.com/?_=1745811422114',
                accept: 'application/json, text/plain, */*',
                'accept-encoding': 'gzip, deflate, br',
                connection: 'keep-alive'
            }
        })
        return rankData.data.result.data;
    }catch(e) {
        console.log(e)
    }
}

const runCateringStore = async (store) => {
    console.log(process.env.WX_STORE_FS_URL + `?_${new Date().getTime()}` + `&snbid=${config[store].userbid}`);
    const d = await ins.get(process.env.WX_STORE_FS_URL + `?_${new Date().getTime()}` + `&snbid=${config[store].userbid}`, {
        headers: {
            'x-request-userbid': config[store].userbid,
            Authorization: process.env.WX_USER_TOKEN,
            cookie: 'SECKEY_ABVK=KJBKlAaMokScslzlV5LbLJOYzoCKHUwibhB3SgTE3yA%3D; BMAP_SECKEY=fdK7zvWCLWIh53DN2F4xq3MUpA1wnHKlihALTkPFiJNxLUqRdOwc5qhOsSBk4PWwq_TmvslMqd_uwQwIfNj_lfcN5jwnfWwtcJEBdiIan2PU1fflEYgCWsaMguX3q4IRvcvecrZmh11sSxlWV0gnj_tcvGPjenj_bqxHUc6RF_7xUuK4COsLd6kxw44cxaQ3',
            snbid: config[store].userbid,
            referer: 'https://qian.54laoban.com/?_=1746329434288'
        }
    })
    // console.log(d);
}


module.exports = {
    runOTA, runArea, runCatering, runRank, runBill, runCateringStore
};